package com.neuedu.oasystem.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neuedu.oasystem.dao.IPrivilegeDao;

import com.neuedu.oasystem.entity.Privilege;
import com.neuedu.oasystem.util.DBHelper;
/**
 * 添加权限
 * 查看所有权限
 * 查找某个权限
 * @author admin
 *
 */
public class PrivilegeDaoImpl extends BaseDao implements IPrivilegeDao{
     /**
      * 添加权限
      */
	@Override
	public boolean add(Privilege param) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		try {
			String sql = "insert into privilege(privilege_id,privilege_name,privilege_description) values(?,?,?)";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, param.getPrivilegeId());
			stmt.setString(2, param.getPrivilegeName());
			stmt.setString(3, param.getPrivilegeDesc());
			
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}
		return false;
	}
	
     //修改权限
	@Override
	public boolean edit(Privilege param) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		try {
			String sql = "update privilege set privilege_name=?,privilege_description=?  where privilege_id=?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(3, param.getPrivilegeId());
			stmt.setString(1, param.getPrivilegeName());
			stmt.setString(2, param.getPrivilegeDesc());
			
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}
		return false;
	}
    //删除权限
	@Override
	public boolean deleteById(int id) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;

		try {
			String sql = "delete from privilege where privilege_Id=?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, id);
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}

		return false;
	}


	@Override
	public Privilege findById(int id) {
		Connection conn =  DBHelper.openConnection();
		PreparedStatement stmt = null;
		Privilege privilege = null;
		ResultSet rs = null;
		
		try {

			String sql = "select  privilege_id,privilege_name,privilege_description from privilege where privilege_id=?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, id);
			rs = stmt.executeQuery();
			if (rs.next()) {
				int privilegeId = rs.getInt(1);
				String privilegeName =rs.getString("privilege_name");
				String privilegeDesc =rs.getString("privilege_description");
				privilege = new Privilege(privilegeId,privilegeName,privilegeDesc);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(rs, stmt, conn);
		}
		return privilege;
	

	}

	@Override
	public List<Privilege> findAll() {
		List<Privilege> list = new ArrayList<Privilege>();
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;

		
		try {
            conn =getConnection();
			String sql = "select  privilege_id,privilege_name,privilege_description from privilege ";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				int privilegeId = rs.getInt("privilege_id");
				String privilegeName =rs.getString("privilege_name");
				String privilegeDesc =rs.getString("privilege_description");
				Privilege privilege = new Privilege(privilegeId,privilegeName,privilegeDesc);
				list.add(privilege);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(rs, stmt, conn);
		}
		return list;
	}

	@Override
	public List<Privilege> findByExample(Privilege param) {
		
		return null;
	}

	@Override
	public List<Privilege> findByPager(int page, int size) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int addAll(List<Privilege> useparamrs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int editAll(List<Privilege> param) {
		// TODO Auto-generated method stub
		return 0;
	}

}
